home *** CD-ROM | disk | FTP | other *** search
/ Aminet 2 / Aminet AMIGA CDROM (1994)(Walnut Creek)[Feb 1994][W.O. 44790-1].iso / Aminet / util / cli / fcmp.lha / fcmp.c next >
Encoding:
C/C++ Source or Header  |  1992-08-31  |  4.9 KB  |  143 lines

  1. /* FCMP Binary File Compare Utility       *
  2.  * by David Champion                      *
  3.  *   FCMP scans two files byte by         *
  4.  *   byte, reporting all differences      *
  5.  *   between the two files with any       *
  6.  *   combination of file byte number,     *
  7.  *   block number/byte offfset, and       *
  8.  *   byte value.  The options are all     *
  9.  *   masked by default, but may be        *
  10.  *   activated with the flags -n (file    *
  11.  *   byte), -b (block/byte offset),       *
  12.  *   and -v (byte value).                 *
  13.  *                                        *
  14.  * Note that this is NOT useful as a      *
  15.  *   binary diff-like utility!            *
  16.  *                                        *
  17.  *   19-Sep-91: -a option added           *
  18.  *   23-May-92: -qq suppresses everything *
  19.  *              only if files equate      */
  20.  
  21. /* includes */
  22. #include <dos/dos.h>
  23. #include <stdio.h>
  24.  
  25. /* defines */
  26.     /* flags */
  27. #define QUIET            0x01
  28. #define BRIEF            0x02
  29. #define VERBOSE            0x04
  30. #define ASCII            0x08
  31. #define HELP            0x10
  32. #define ULTRAQUIET        0x20
  33.  
  34.     /* intelligibility */
  35.         /* parameters */
  36. #define COLOR            "\033[33m"
  37. #define PLAIN            "\033[0m"
  38. #define QUICK            0
  39. #define FULL            1
  40.         /* functions */
  41. #define EQ                !strcmp
  42.     /* macros */
  43. #define MAKEHEX(a)    ((a)>9?(a)+55:(a)+48)
  44.  
  45. main(argc,argv)
  46. int argc;
  47. char *argv[];
  48. {
  49. int c, d, k=0, n=0;
  50. char flags=0;
  51. FILE *file1, *file2;
  52.  
  53.     if ((argc < 2) || (argc > 5)) Usage(argv[0],QUICK);    /* if bad syntax, show quick usage */
  54.     for (c=1;c<argc;c++) {                    /* better not to have useless variables--so use one */
  55.                                 /* that's not yet serving its true purpose.         */
  56.         if (EQ(argv[c],"-v")) flags=flags | VERBOSE;
  57.         if (EQ(argv[c],"-a")) flags=flags | ASCII;
  58.         if (EQ(argv[c],"-h")) flags=flags | HELP;
  59.         if (EQ(argv[c],"-q")) flags=flags | QUIET;
  60.         if (EQ(argv[c],"-b")) flags=flags | BRIEF;
  61.         if (EQ(argv[c],"-qq")) flags=flags | ULTRAQUIET;
  62.     }
  63.     /*if (flags >= HELP) flags=HELP;*/            /* if Help flagged, nothing else allowed (strictly for */
  64.                                 /* illustration; if help is flagged, it doesn't matter */
  65.                                 /* what else is, since we just show help and exit.     */
  66.     if (flags >= ASCII) flags |= VERBOSE;            /* if ASCII flagged, must have verbose also */
  67.     if (flags >= VERBOSE) flags &= ~QUIET;            /* if Verbose flagged, can't have Quiet too */
  68.     if (flags >= ULTRAQUIET) flags |= QUIET;        /* if ultraquiet, gotta be quiet too */
  69.  
  70.     if ((flags & HELP) == HELP) Usage(argv[0],FULL);    /* if Help set, display full usage */
  71.  
  72.     if ((file1 = fopen(argv[argc-2], "r")) == NULL) NoOpen(argv[argc-2]);
  73.     if ((file2 = fopen(argv[argc-1], "r")) == NULL) {
  74.         fclose(file1);
  75.         NoOpen(argv[argc-1]);
  76.     }
  77.  
  78.     if ((flags & ULTRAQUIET) != ULTRAQUIET) {
  79.         printf("%s simple file compare utility by D. Champion.\n",argv[0]);
  80.         printf("Scanning %s against %s...\n",argv[argc-2],argv[argc-1]);
  81.     }
  82.  
  83.     while (((c=getc(file1)) != EOF) && ((d=getc(file2)) != EOF)) {
  84.         if ( SetSignal(0L, SIGBREAKF_CTRL_C) & SIGBREAKF_CTRL_C ) {
  85.             printf("\nUser break.\n");
  86.             break;
  87.         }
  88.         if (c != d) {
  89.             if ((flags & QUIET) != QUIET) {    /* Quiet flag not set */
  90.                 printf("\nMismatch found at file byte %d",k);
  91.                 if ((flags & BRIEF) != BRIEF)    /* Brief flag not set */
  92.                          printf(" (block %d, offset %d)",k/512+1,k%512);
  93.                 if ((flags & VERBOSE) == VERBOSE) {    /* Verbose flag set */
  94.                         printf(" \t: %c%c",MAKEHEX(c/16),MAKEHEX(c%16));
  95.                         if ((flags & ASCII) == ASCII)    /* ASCII representation */
  96.                                 printf(" %c",c);
  97.                         printf(" : %c%c",MAKEHEX(d/16),MAKEHEX(d%16));
  98.                         if ((flags & ASCII) == ASCII)    /* ASCII representation */
  99.                                 printf(" %c",d);
  100.                 }
  101.             }
  102.             n++;
  103.         }
  104.         k++;
  105.     }
  106.  
  107.     fclose(file1);
  108.     fclose(file2);
  109.     if ((flags & ULTRAQUIET) != ULTRAQUIET) printf("\n%d bytes checked, %d differences found.\n",k,n);
  110.     else printf("Files not equal.\n");
  111. }
  112.  
  113. Usage(myname,degree)
  114. int degree;
  115. char *myname;
  116. {
  117.     printf("Usage: %s [-h] | [ [-v [-a] | -q | -qq] [-b] file1 file2 ]\n",myname);
  118.     if (degree) {
  119.         printf("       "COLOR"%s"PLAIN" reports any differences between "COLOR"file1"PLAIN
  120.                 " and "COLOR"file2"PLAIN"\n",myname);
  121.         printf("       \tto the limit of the shorter of the two files.\n");
  122.         printf("       Options:\n");
  123.         printf("       \t"COLOR"-h"PLAIN"  help:\tthis display\n");
  124.         printf("       \t"COLOR"-v"PLAIN"  verbose:\tshow hexadecimal diffs\n");
  125.         printf("       \t"COLOR"-a"PLAIN"  ASCII:\tshow ascii character too\n");
  126.         printf("       \t"COLOR"-q"PLAIN"  quiet:\tshow only post-scan totals\n");
  127.         printf("       \t"COLOR"-qq"PLAIN" ultraquiet:\tshow nothing if equal\n");
  128.         printf("       \t"COLOR"-b"PLAIN"  brief:\tshow only byte numbers\n");
  129.         printf("       When mutually exclusive options are requested, priority is given\n");
  130.         printf("       \tto the one listed first in the syntax line above:\n");
  131.         printf("       \t   %s -v -b -q src.bak src   -->   %s -v -b src.bak src\n",myname,myname);
  132.         printf("       \t   %s -q -h copy orig        -->   %s -h\n",myname,myname);
  133.     }
  134.     exit(5);
  135. }
  136.  
  137. NoOpen(name)
  138. char *name;
  139. {
  140.     printf("Can't open file %s for input.\n",name);
  141.     exit (10);
  142. }
  143.